package com.atguigu.juc.t14;

import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class T3 {
    public static void main(String[] args) {
        Semaphore parking = new Semaphore(3);
        for (int i = 1; i < 8; i++) {
            new Thread(()->{
                try {
                    parking.acquire();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "抢到了车位");
                try {
                    TimeUnit.SECONDS.sleep(new Random().nextInt(3) + 1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName() + "停车完毕");
                parking.release();
                System.out.println(Thread.currentThread().getName() + "驶出车位");
            }).start();
        }
    }
}
